package com.jxwifi.cloud.quickclean.adapter;

import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.RatingBar;

import com.amap.api.maps.AMapUtils;
import com.amap.api.maps.model.LatLng;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.idroid.widget.CircleImageView;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.app.LocationGaoDe;
import com.jxwifi.cloud.quickclean.app.StateUtils;
import com.jxwifi.cloud.quickclean.bean.HomeaddressBean;
import com.jxwifi.cloud.quickclean.bean.MyCleanersBean;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by Administrator on 2019/6/11.
 */

public class CleanerListAdapter extends BaseQuickAdapter<MyCleanersBean, BaseViewHolder> {


    private String TAG = this.getClass().getSimpleName();
    private boolean isCollection;
    public CleanerListAdapter(@LayoutRes int layoutResId) {
        super(layoutResId, null);
    }

    public CleanerListAdapter(@LayoutRes int layoutResId, @Nullable List<MyCleanersBean> data, boolean isCollection) {
        super(layoutResId, data);
        this.isCollection = isCollection;
    }

    @Override
    protected void convert(BaseViewHolder helper, MyCleanersBean item) {

        Glide.with(mContext).load(item.getHeadImg()).into((CircleImageView)helper.getView(R.id.img_personal_data_photo));

        //double disprovn = DistanceOfTwoPoints(Double.valueOf(StateUtils.AddLat), Double.valueOf(StateUtils.AddLon), Double.valueOf(item.getLat()), Double.valueOf(item.getLon())) / 1000;
        //double disprovn = DistanceOfTwoPoints(Float.parseFloat(StateUtils.AddLat), Float.parseFloat(StateUtils.AddLon), Float.parseFloat(item.getLat()), Float.parseFloat(item.getLon())) / 1000;
        if (item.getRideDistance() > 1) {
            DecimalFormat format = new DecimalFormat("#0.00");
            String str = format.format(item.getRideDistance());
            helper.setText(R.id.tv_list_cleaner,str+"公里");
        } else {
            helper.setText(R.id.tv_list_cleaner,"<1公里");
        }
        helper.setText(R.id.tv_cleaner_name,item.getRealName());
        helper.setText(R.id.tv_cleaner_age,"年龄："+item.getAge()+"岁");
        helper.setText(R.id.tv_cleaner_identity,"身份证号："+item.getIdCardNum());
        RatingBar mrat=helper.getView(R.id.rb_shop_details_list);
        mrat.setRating((float) (item.getGrade() / 20));

        helper.addOnClickListener(R.id.rel_list_cleaner_items);
        if(isCollection){
            helper.getView(R.id.img_collection_my_cleaner).setVisibility(View.VISIBLE);
            helper.getView(R.id.img_share_cleaner).setVisibility(View.VISIBLE);
            helper.addOnClickListener(R.id.img_collection_my_cleaner);
            helper.addOnClickListener(R.id.img_share_cleaner);
        }

        if (StateUtils.TRUE.equals(item.getIsInDispatch())){ //判断是否符合接单条件.为false显示msg字段不可点击
            helper.getView(R.id.rel_my_cleaning_starts).setVisibility(View.GONE);
            helper.getView(R.id.tv_my_cleaning_starts).setVisibility(View.GONE);
        }else{
            helper.getView(R.id.rel_my_cleaning_starts).setVisibility(View.VISIBLE);
            helper.getView(R.id.tv_my_cleaning_starts).setVisibility(View.VISIBLE);
            helper.setText(R.id.tv_my_cleaning_starts,item.getMsg());
        }
    }

    private static final double EARTH_RADIUS = 6378137;

    public static double DistanceOfTwoPoints(double lat1, double lng1,
                                             double lat2, double lng2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;

        /*LatLng latLng = new LatLng(lat1,lng1);
        LatLng latLng2 = new LatLng(lat2,lng2);
        float distance = AMapUtils.calculateLineDistance(latLng,latLng2);
        BigDecimal b = new BigDecimal(String.valueOf(distance));
        double s = b.doubleValue();*/
        return s;
    }

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

}
